package com.sizu.web;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sizu.common.saledto.TSaleListDto;
import com.sizu.common.utils.BlandUtil;
import com.sizu.common.vo.ResultEntity;
import com.sizu.entity.TSaleList;
import com.sizu.entity.TSaleListGoods;
import com.sizu.entity.TSaleListZyx;
import com.sizu.service.ITSaleListGoodsService;
import com.sizu.service.ITSaleListService;
import com.sizu.service.SyncService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 销售Controller
 * </p>
 *
 * @author 赵玉玺
 * @since 2020-02-26
 */
@RestController
@RequestMapping("/sale/tSaleList")
public class TSaleListController {

    @Resource
    private ITSaleListService itSaleListService;

    @Resource
    private ITSaleListGoodsService itSaleListGoodsService;

    @Autowired
    private SyncService syncService;

    /**
     * 查询出售列表
     *
     * @param tSaleListDto
     * @return
     */
    @RequestMapping("/list")
    public ResultEntity tSaleListList(TSaleListDto tSaleListDto) {
        TSaleListZyx tSaleListZyx = new TSaleListZyx();
        BeanUtils.copyProperties(tSaleListDto, tSaleListZyx);
        List<TSaleListZyx> tSaleListZyx1 = itSaleListService.getTSaleListZyx(tSaleListZyx);
        return ResultEntity.ok("查询成功", tSaleListZyx1);
    }

    /**
     * 添加方法
     *
     * @param tSaleListDto
     * @return
     */
    @RequestMapping("/add")
    public ResultEntity tSaleListAdd(TSaleListDto tSaleListDto) {
        TSaleList tSaleList = new TSaleList();
        BeanUtils.copyProperties(tSaleListDto, tSaleList);
        itSaleListService.insert(tSaleList);

        tSaleListDto.setId(tSaleList.getId());
        syncService.syncSale(tSaleListDto);
        //添加多的一方
        if (!StringUtils.isBlank(tSaleListDto.getSaleListGoodsJson())) {
            List<TSaleListGoods> tSaleListGoods = JSON.parseArray(tSaleListDto.getSaleListGoodsJson(), TSaleListGoods.class);
            for (TSaleListGoods tSaleListGood : tSaleListGoods) {
                tSaleListGood.setSaleListId(tSaleList.getId());
                itSaleListGoodsService.insert(tSaleListGood);
            }
        }

        return ResultEntity.ok("添加成功");

    }

    /**
     * 修改方法
     *
     * @param tSaleListDto
     * @return
     */
    @RequestMapping("/up")
    public ResultEntity tSaleListUp(TSaleListDto tSaleListDto) {
        TSaleList tSaleList = new TSaleList();
        BeanUtils.copyProperties(tSaleListDto, tSaleList);
        itSaleListService.updateById(tSaleList);
        //添加多的一方
        if (!StringUtils.isBlank(tSaleListDto.getSaleListGoodsJson())) {
            Wrapper<TSaleListGoods> wrapper1 = new EntityWrapper<>();
            wrapper1.eq("sale_list_id", tSaleListDto.getId());
            itSaleListGoodsService.delete(wrapper1);
            Wrapper<TSaleList> wrapper = new EntityWrapper<>(tSaleList);
            TSaleList tSaleList1 = itSaleListService.selectOne(wrapper);
            List<TSaleListGoods> tSaleListGoods = JSON.parseArray(tSaleListDto.getSaleListGoodsJson(), TSaleListGoods.class);
            for (TSaleListGoods tSaleListGood : tSaleListGoods) {
                tSaleListGood.setSaleListId(tSaleList1.getId());
                itSaleListGoodsService.insert(tSaleListGood);
            }
        }
        return ResultEntity.ok("修改成功");
    }

    /**
     * 删除方法
     *
     * @param tSaleListDto
     * @return
     */
    @RequestMapping("/dels")
    public ResultEntity tSaleListDels(TSaleListDto tSaleListDto) {
        for (Integer id : tSaleListDto.getIds()) {
            itSaleListService.deleteById(id);
            Wrapper<TSaleListGoods> wrapper1 = new EntityWrapper<>();
            wrapper1.eq("sale_list_id", tSaleListDto.getId());
            itSaleListGoodsService.delete(wrapper1);
        }
        return ResultEntity.ok("删除成功");
    }

    /**
     * 返回随机编号
     *
     * @return
     */
    @RequestMapping("/number")
    public ResultEntity getOrderNumber() {
        return ResultEntity.ok("获取成功", "XS" + System.currentTimeMillis());
    }

}
